<?php
require_once(dirname(__FILE__)."/config.php");
CheckPurview('api_passport');
require_once(RSMSINC.'/rstemplate.class.php');
if(defined('UC_API'))
{
	ShowMsg('您已安装UCenter模块,不能使用该功能!',-1); exit();	
}

$dopost = api_gpc('dopost','R');

$api = new api_passport($dopost);

class api_passport
{
	var $action;
	var $dtp;
	var $config;
	
	//php5构造函数PHP>=5.0
	function __construct($ac = '')
	{
		$action = 'pp_'.(empty($ac)||(!in_array($ac,array('install','edit'))) ? 'show' : trim($ac));
		$this->dtp = new RsmsTemplate();
		$this->config = RSMSINC.'/common.inc.php';
		$this->$action();
	}
	
	//构造类成员PHP<5.0
	function api_passport($ac = '')
	{
		$this->__construct($ac);
	}
	
	function pp_install()
	{
		$pp_setings = api_gpc('pp_setings','P');
		
		if(!isset($pp_setings['ppname']) || empty($pp_setings['ppname']))
		{
			ShowMsg('请选择一个可用的应用接口!',-1); exit();
		}
		
		if(empty($pp_setings['ppkey']))
		{
			ShowMsg('请填写一个通行密钥!',-1); exit();
		}
		
		//默认不开启通行证接口
		$pp_setings['ppopen'] = !$pp_setings['ppopen'] ? 0 : 1;
		
		$fp = fopen($this->config, 'r');
		$content = fread($fp, filesize($this->config));
		$content = trim($content);
		$content = substr($content, -2) == '?>' ? substr($content, 0, -2) : $content;

		$pp_setings['pptype'] = $pp_setings['pptype'] == 'server' ? 'server' : 'client';
		
		if($pp_setings['pptype'] == 'client')
		{
			$pp_setings['ppapi'] = $pp_setings['ppname'] == 'phpwind' ? ($pp_setings['ppapi'].'/passport_client.php') : ($pp_setings['ppapi'].'/api/passport.php');
		}else
		{
			$pp_setings['ppapi'] = str_replace("/passport_client.php",'',$pp_setings['ppapi']);
			$pp_setings['ppapi'] = str_replace("/api/passport.php",'',$pp_setings['ppapi']);
		}

		$pp_setings['pplogin'] = !isset($pp_setings['pplogin']) ? '' : $pp_setings['pplogin'];
		$pp_setings['pplogout'] = !isset($pp_setings['pplogout']) ? '' : $pp_setings['pplogout'];
		$pp_setings['ppreg'] = !isset($pp_setings['ppreg']) ? '' : $pp_setings['ppreg'];
		$pp_setings['ppapi'] = !isset($pp_setings['ppapi']) ? '' : $pp_setings['ppapi'];
		
		$content = $this->insert_config($content, "/define\('PP_API',\s*'.*?'\);/i", "define('PP_API', '".$pp_setings['ppapi']."');");
		$content = $this->insert_config($content, "/define\('PP_NAME',\s*'.*?'\);/i", "define('PP_NAME', '".$pp_setings['ppname']."');");
		$content = $this->insert_config($content, "/define\('PP_LOGIN',\s*'.*?'\);/i", "define('PP_LOGIN', '".$pp_setings['pplogin']."');");
		$content = $this->insert_config($content, "/define\('PP_LOGOUT',\s*'.*?'\);/i", "define('PP_LOGOUT', '".$pp_setings['pplogout']."');");
		$content = $this->insert_config($content, "/define\('PP_REG',\s*'.*?'\);/i", "define('PP_REG', '".$pp_setings['ppreg']."');");
		$content = $this->insert_config($content, "/define\('PP_KEY',\s*'.*?'\);/i", "define('PP_KEY', '".$pp_setings['ppkey']."');");
		$content = $this->insert_config($content, "/define\('PP_OPEN',\s*'.*?'\);/i", "define('PP_OPEN', '".$pp_setings['ppopen']."');");
		$content = $this->insert_config($content, "/define\('PP_TYPE',\s*'.*?'\);/i", "define('PP_TYPE', '".$pp_setings['pptype']."');");
		$content .= "\r\n".'?>';
		
		if($fp = @fopen($this->config, 'w'))
		{
			@fwrite($fp, trim($content));
			@fclose($fp);
			ShowMsg('配置已经生效!',-1); exit();
		}
		else
		{
			ShowMsg('写入配置数据失败!'.$this->config.' 请设置可写权限!',-1); exit();
		}
	}
	
	function pp_edit()
	{
		$pp_setings = api_gpc('pp_setings','P');

		if(empty($pp_setings['ppname']))
		{
			ShowMsg('请选择一个可用的应用接口!',-1); exit();
		}
		
		if(empty($pp_setings['ppkey']))
		{
			ShowMsg('请填写一个通行密钥!',-1); exit();
		}
		
		$pp_setings['ppopen'] = !$pp_setings['ppopen'] ? 0 : 1;
		
		$pp_setings['pptype'] = $pp_setings['pptype'] == 'server' ? 'server' : 'client';

		$pp_setings['pplogin'] = empty($pp_setings['pplogin']) ? PP_LOGIN : $pp_setings['pplogin'];
		$pp_setings['pplogout'] = empty($pp_setings['pplogout']) ? PP_LOGOUT : $pp_setings['pplogout'];
		$pp_setings['ppreg'] = empty($pp_setings['ppreg']) ? PP_REG : $pp_setings['ppreg'];
		$pp_setings['ppapi'] = empty($pp_setings['ppapi']) ? PP_API : $pp_setings['ppapi'];
		
		if($pp_setings['pptype'] == 'client')
		{
			$pp_setings['ppapi'] = str_replace("/passport_client.php",'',$pp_setings['ppapi']);
			$pp_setings['ppapi'] = str_replace("/api/passport.php",'',$pp_setings['ppapi']);			
			$pp_setings['ppapi'] = $pp_setings['ppname'] == 'phpwind' ? ($pp_setings['ppapi'].'/passport_client.php') : ($pp_setings['ppapi'].'/api/passport.php');
		}
		else
		{
			$pp_setings['ppapi'] = str_replace("/passport_client.php",'',$pp_setings['ppapi']);
			$pp_setings['ppapi'] = str_replace("/api/passport.php",'',$pp_setings['ppapi']);
		}
		
		$fp = fopen($this->config, 'r');
		$content = fread($fp, filesize($this->config));
		$content = trim($content);
		$content = substr($content, -2) == '?>' ? substr($content, 0, -2) : $content;
		
		$content = $this->insert_config($content, "/define\('PP_API',\s*'.*?'\);/i", "define('PP_API', '".$pp_setings['ppapi']."');");
		$content = $this->insert_config($content, "/define\('PP_NAME',\s*'.*?'\);/i", "define('PP_NAME', '".$pp_setings['ppname']."');");
		$content = $this->insert_config($content, "/define\('PP_LOGIN',\s*'.*?'\);/i", "define('PP_LOGIN', '".$pp_setings['pplogin']."');");
		$content = $this->insert_config($content, "/define\('PP_LOGOUT',\s*'.*?'\);/i", "define('PP_LOGOUT', '".$pp_setings['pplogout']."');");
		$content = $this->insert_config($content, "/define\('PP_REG',\s*'.*?'\);/i", "define('PP_REG', '".$pp_setings['ppreg']."');");
		$content = $this->insert_config($content, "/define\('PP_KEY',\s*'.*?'\);/i", "define('PP_KEY', '".$pp_setings['ppkey']."');");
		$content = $this->insert_config($content, "/define\('PP_OPEN',\s*'.*?'\);/i", "define('PP_OPEN', '".$pp_setings['ppopen']."');");
		$content = $this->insert_config($content, "/define\('PP_TYPE',\s*'.*?'\);/i", "define('PP_TYPE', '".$pp_setings['pptype']."');");
		$content .= '?>';
		
		if($fp = @fopen($this->config, 'w'))
		{
			@fwrite($fp, trim($content));
			@fclose($fp);
			ShowMsg('配置已经更改!',-1); exit();
		}
		else
		{
			ShowMsg('写入配置数据失败!'.$this->config.' 请设置可写权限!',-1); exit();
		}
	}
	
	function pp_show()
	{
		$this->dtp->Assign('pp_config_file',$this->config);
		
		if(!defined('PP_API'))
		{
			$this->dtp->LoadTemplate(RSMSADMIN.'/templets/api_passport_install.htm');
		}
		else
		{
			$pp_setings = array('ppname' => PP_NAME, 'ppapi' => PP_API, 'pplogin' => PP_LOGIN, 'pplogout' => PP_LOGOUT, 'ppreg' => PP_REG,'ppkey' => PP_KEY, 'ppopen' => PP_OPEN, 'pptype' => PP_TYPE);
			
			if($pp_setings['pptype'] == 'client')
			{
				$pp_setings['ppapi'] = str_replace("/passport_client.php",'',$pp_setings['ppapi']);
				$pp_setings['ppapi'] = str_replace("/api/passport.php",'',$pp_setings['ppapi']);
			}
			
			$this->dtp->Assign('pp_setings',$pp_setings);
			$this->dtp->LoadTemplate(RSMSADMIN.'/templets/api_passport_edit.htm');
		}
		$this->dtp->Display(); exit();
	}
	
	
	function insert_config($s, $find, $replace)
	{
		if(preg_match($find, $s))
		{
			$s = preg_replace($find, $replace, $s);
		}else{
			// 插入到最后一行
			$s .= "\r\n".$replace;
		}
		return $s;
	}
}


function api_gpc($k, $var='R')
{
	switch($var)
	{
		case 'G': $var = &$_GET; break;
		case 'P': $var = &$_POST; break;
		case 'C': $var = &$_COOKIE; break;
		case 'R': $var = &$_REQUEST; break;
	}
	return isset($var[$k]) ? $var[$k] : NULL;
}

if(!function_exists('file_put_contents')){ function file_put_contents($filename, $s)
{
	$fp = @fopen($filename, 'w');
	@fwrite($fp, $s);
	@fclose($fp);
	return TRUE;
}}
?>